//
//  ESRegisterVC.m
//  Eyeshot
//
//  Created by admin on 15/7/12.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESRegisterVC.h"

@interface ESRegisterVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UITextField *passwd;
@property (weak, nonatomic) IBOutlet UITextField *ackPasswd;

@end

@implementation ESRegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _userName.delegate = self;
    _passwd.delegate = self;
    _email.delegate = self;
    _ackPasswd.delegate = self;
    _passwd.secureTextEntry = YES;
    _ackPasswd.secureTextEntry = YES;
    _userName.tintColor = [UIColor whiteColor];
    _passwd.tintColor = [UIColor whiteColor];
    _email.tintColor = [UIColor whiteColor];
    _ackPasswd.tintColor = [UIColor whiteColor];
    
    _userName.attributedPlaceholder = [self attributedString:@"用户名" fontOfSize:16 color:[UIColor grayColor]];
    _passwd.attributedPlaceholder = [self attributedString:@"密码" fontOfSize:16 color:[UIColor grayColor]];
    _email.attributedPlaceholder = [self attributedString:@"邮箱" fontOfSize:16 color:[UIColor grayColor]];
    _ackPasswd.attributedPlaceholder = [self attributedString:@"密码确认" fontOfSize:16 color:[UIColor grayColor]];
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - UI Action

- (IBAction)register:(UIButton *)sender {
    
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:_userName.text];
    [bUser setPassword:_passwd.text];
    [bUser setEmail:_email.text];
    
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
//            NSLog(@"%@ 注册成功",bUser);
            [self dismissViewControllerAnimated:YES completion:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UserOK" object:nil userInfo:nil];
        } else {
//            NSLog(@"%@",error);
        }
    }];
}

- (IBAction)hasAccount:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)cancel:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - delegate


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.attributedPlaceholder = [self attributedString:textField.placeholder fontOfSize:16 color:[UIColor whiteColor]];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.attributedPlaceholder = [self attributedString:textField.placeholder fontOfSize:16 color:[UIColor grayColor]];
}




- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - other
- (NSAttributedString *) attributedString:(NSString *)string fontOfSize:(NSInteger)size color:(UIColor *)color {
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:size],NSForegroundColorAttributeName :color};
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:dict];
    return attrString;
}

@end
